package com.yuan.blog.Interceptor;

import com.yuan.blog.entity.UserEntity;
import com.yuan.blog.exception.JsonException;
import com.yuan.blog.exception.ViewException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: blog
 * @description:
 * @author: yuan_boss
 * @create: 2022-12-19 19:39
 **/
@Component
public class BlogInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserEntity userEntity = (UserEntity)request.getSession().getAttribute("loginUser");
        if (userEntity == null){
            throw new ViewException(100,"view/site/login");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
